$(function(){
	//ajaxform
	var options = { 
		dataType:'json',
		beforeSubmit:showRequest,//表单验证方式
        success:showResponse  // post-submit callback 
    }; 
    $('#ajaxForm').ajaxForm(options); 
    function showResponse(responseText, statusText)  {
    	$('#ajaxForm button').removeAttr('disabled');
    	$('#ajaxForm input').removeAttr('disabled');
		if (statusText == 'success') {
			parent.layer.close(load);
			if (responseText.status==1) {
				parent.layer.open({
					type:0, 
					icon:1,
					content:responseText.info,
					time:3000,
					end: function(){ 
						if(responseText.url!='' && responseText.url!=undefined && responseText.url!="undefined"){
							if (responseText.url=='reload') {
								window.location.reload();
							}else{
								window.location.href = responseText.url;
							}									
						}
					} 
				});
			}else{
				parent.layer.open({
					type:0, 
					icon:2,
					content:responseText.info
				});
			}
		}else{
			parent.layer.msg('服务器错误');
		}
	} 

	function showRequest(){
		$('#ajaxForm button').attr("disabled","disabled");
		$('#ajaxForm input').attr("disabled","disabled");
		load = parent.layer.load(0,{shade: [0.7, '#000000']});
	}
})

function uploadfile(url,o){
	$("#fileupload").click();
	//文件上传js
	var btn = $(".uploadbtn");
	var upload="";
	$("#myupload").attr("action",url);
	$("#fileupload").bind("change",function(){
		if($(this).val()!=""){
			$("#myupload").ajaxSubmit({
				dataType:  'json',
				beforeSend: function() {
					btn.val("上传中...");
					upload=layer.open({
					  type: 1,
					  title: false,
					  closeBtn: 1,
					  shadeClose: false,
					  area: ['420px', '100px'],
					  content: $('#uploading')
					});
	    		},
	    		uploadProgress: function(event, position, total, percentComplete) {
	        		var percentVal = percentComplete + '%';
	        		var left=$(".load-bar").width()*percentComplete/100;
	        		$("#counter").html(percentVal).css("left",(left-35)+"px");
	        		$(".load-bar-inner").css("width",left+"px");
	        		$("#filesize").val(percentVal);

	    		},
				success: function(data) {
					$("#fileupload").unbind("change");
					layer.close(upload);
					$('#uploading').css("display","none");
					btn.val("本地上传");
					$("#myupload")[0].reset();
					if(data.state=="SUCCESS"){
						$("#"+o+"_url").val(data.url);
						$("#"+o+"_thumb_url").val(data.thumb);
						$("#"+o+"_size").val(data.size);
						$("#"+o+"_src").attr("src",data.url);
					}else{
						layer.msg(data.state, {time:2000, icon:2});
					}
				},
				error:function(xhr){
					btn.val("上传失败");
				}
			})
		}
	})
}

function uploadMultiFile(url, o) {

	/*if ($(".insert-img li").length>=4){
		parent.layer.open({type:0, icon:2,content:'最多上传4张'});
		return;
	}*/

	$("#fileupload").click();
	//文件上传js
	var btn = $(".uploadbtn");
	var upload = "";
	$("#myupload").attr("action", url);
	$("#fileupload").bind("change", function() {
		if ($(this).val() != "") {
			$("#myupload").ajaxSubmit({
				dataType: 'json',
				beforeSend: function() {
					btn.val("上传中...");
					upload = layer.open({
						type: 1,
						title: false,
						closeBtn: 1,
						shadeClose: false,
						area: ['420px', '100px'],
						content: $('#uploading')
					});
				},
				uploadProgress: function(event, position, total, percentComplete) {
					var percentVal = percentComplete + '%';
					var left = $(".load-bar").width() * percentComplete / 100;
					$("#counter").html(percentVal).css("left", (left - 35) + "px");
					$(".load-bar-inner").css("width", left + "px");
					$("#filesize").val(percentVal);

				},
				success: function(data) {

					$("#fileupload").unbind("change");
					layer.close(upload);
					$('#uploading').css("display", "none");
					btn.val("本地上传");
					$("#myupload")[0].reset();
					if (data.state == "SUCCESS") {
						_html = '<li><img src="'+data.url+'" /><input type="hidden" name="image[]" value="'+data.url+'" /><i class="fa fa-close" onclick="removeLi(this)"></i> <a href="javascript:void(0)" onclick="setFace(this,\'face\',\''+data.url+'\')">设为封面</a></li>';
						$(".insert-btn").before(_html);
					} else {
						layer.msg(data.state, {
							time: 2000,
							icon: 2
						});
					}
				},
				error: function(xhr) {
					btn.val("上传失败");
				}
			})
		}
	})
}

function setFace(obj,to,str){
	$("#"+to).val(str);
	$(".insert-img li a").removeClass("active");
	$(obj).addClass("active");
}

function removeLi(obj) {
	console.info($(obj).parent());
	$(obj).parent().remove();
}

//重载验证码
function fleshVerify(){	
	var timenow = new Date().getTime();
	$('#verifyImg').attr("src", '/index.php?m=Adminx&c=Login&a=verify&temp='+timenow);
}

//载入各种模块
$(function(){
	layui.config({
		base: '/tpl/static/layui/modules/'
	});

	layui.use(['icheck','form','laydate'], function() {
		var $ = layui.jquery;
		$('table input').iCheck({
			checkboxClass: 'icheckbox_flat-green'
		});

		$('.site-table tbody tr').on('click', function(event) {
			var $this = $(this);
			var $input = $this.children('td').eq(0).find('input');
			$input.on('ifChecked', function(e) {
				$this.css('background-color', '#EEEEEE');
			});
			$input.on('ifUnchecked', function(e) {
				$this.removeAttr('style');
			});
			$input.iCheck('toggle');
		}).find('input').each(function(){
			var $this = $(this);
			$this.on('ifChecked', function(e) {
				$this.parents('tr').css('background-color', '#EEEEEE');
			});
			$this.on('ifUnchecked', function(e) {
				$this.parents('tr').removeAttr('style');
			});
		});
		$('#selected-all').on('ifChanged', function(event) {
			var $input = $('.site-table tbody tr td').find('input');
			$input.iCheck(event.currentTarget.checked ? 'check' : 'uncheck');
		});

		//表单验证
		var form = layui.form();
		//自定义验证规则
		form.verify({
			_mobile: function(value) {
				if(value !='') {
					if (!checkMobile(value)) {
						return '请输入正确的手机号码';
					}
				}
			},
			__mobile: function(value) {
				if (!checkMobile(value)) {
					return '请输入正确的手机号码';
				}
			},
			_url: function(value) {
				if(value !='') {
					if (!checkUrl(value)) {
						return '请输入正确URL格式';
					}
				}
			},
			__url: function(value) {
				if (!checkUrl(value)) {
					return '请输入正确URL格式';
				}
			},
			__username: function(value) {
				if (!checkWordLong(value,2,8)) {
					return '请输入用户名2-8个字符';
				}
			},
			_password: function(value) {
				if(value !='') {
					if (!checkWordLong(value,6,12)) {
						return '请输入密码6-12个字符';
					}
				}
			},
			__password: function(value) {
				if (!checkWordLong(value,6,12)) {
					return '请输入密码6-12个字符';
				}
			},
			__repassword: function(value) {
				if (!checkRepassword(value)) {
					return '两次密码不同';
				}
			}
		});
	});
})

$(function(){
	$("#delBtn").click(function(){
		layer.msg('确定删除吗？', {
			time:0,
			btn: ['确定', '取消'],
			yes: function(index){
		    	layer.close(index);
		    	$("#ajaxForm").submit();
			}
		});
	});

	$("#restoreBtn").click(function(){
    	$("#ajaxForm").attr("action", $(this).attr("url"));
    	$("#ajaxForm").submit();
	});
})

function delImage(domid){
	$("#"+domid+"_url").val("");
	$("#"+domid+"_src").attr('src','/tpl/static/image/image.jpg');
}

function openModel (url,title,width,height) {
	layer.open({
		type: 2,
		title: title,
		shadeClose: true,
		shade: 0.8,
		area: [width, height],
		content: url //iframe的url
	}); 
}

//清理缓存
function clearcache(){
	layer.msg('确定清楚缓存吗？', {
		time:0,
		btn: ['确定', '取消'],
		yes: function(index){
			layer.close(index);
	    	layer.load(0, {shade:[0.5,'#000'],time:0});
	    	$.ajax({
	    		url:"/index.php?m=Adminx&c=Index&a=clearcache",
	    		success:function(data){
	    			layer.closeAll();
	    			if(data.state==1){
	    				layer.alert(data.info, {icon:0});
	    			}else{
	    				layer.alert(data.info, {icon:1});
	    			}
	    		}
	    	});
		}
	});
}

$(function(){
	$(".tx-btn").on("click",function () {
		var o=$(this),url = $(this).attr("data-url"), id=$(this).attr("data-id");
		layer.msg('确认已经处理吗？', {
			time:0,
			btn: ['确定', '取消'],
			yes: function(index){
		    	layer.close(index);
			    $.post(url, {id: id}, function (data) {
			    	if (data.status==1) {
			    		o.remove();
			    		$("#td"+id).html("已处理");
			    	}else{
			        	layer.alert(data.info, {icon: 5});
			      	}
			    }, "json");
			    return false;
			}
		});
	});
})